package ph.com.gs3.sockettest2;

import android.app.Activity;
import android.os.Bundle;
import android.util.Log;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.TextView;

import com.koushikdutta.async.http.AsyncHttpClient;
import com.koushikdutta.async.http.socketio.Acknowledge;
import com.koushikdutta.async.http.socketio.ConnectCallback;
import com.koushikdutta.async.http.socketio.SocketIOClient;
import com.koushikdutta.async.http.socketio.StringCallback;

public class MainActivity extends Activity {

	public static final String TAG = MainActivity.class.getSimpleName();

	private TextView tvMessages;
	private Button bSendMessage;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_main);

		initializeViews();
		initializeSocket();
	}

	@Override
	public boolean onCreateOptionsMenu(Menu menu) {
		// Inflate the menu; this adds items to the action bar if it is present.
		getMenuInflater().inflate(R.menu.main, menu);
		return true;
	}

	@Override
	public boolean onOptionsItemSelected(MenuItem item) {
		// Handle action bar item clicks here. The action bar will
		// automatically handle clicks on the Home/Up button, so long
		// as you specify a parent activity in AndroidManifest.xml.
		int id = item.getItemId();
		if (id == R.id.action_settings) {
			return true;
		}
		return super.onOptionsItemSelected(item);
	}

	private void initializeViews() {

		tvMessages = (TextView) findViewById(R.id.Main_tvMessages);
		bSendMessage = (Button) findViewById(R.id.Main_bSend);
		bSendMessage.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View v) {
				tvMessages.append("\nYou: sent test message");
			}
		});

	}

	private void initializeSocket() {

		SocketIOClient.connect(AsyncHttpClient.getDefaultInstance(), "http://192.168.0.69:3013",
		        new ConnectCallback() {

			        @Override
			        public void onConnectCompleted(Exception ex, SocketIOClient client) {

				        if (ex != null) {
					        ex.printStackTrace();
					        return;
				        }

				        client.setStringCallback(new StringCallback() {

					        @Override
					        public void onString(String string, Acknowledge acknowledge) {
						        Log.v(TAG, string);
					        }
				        });

				        // client.on("", callback);

			        }
		        });

	}
}
